/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmProcedureBill.java
 *
 * Created on Mar 17, 2011, 10:34:31 PM
 */
package inventorymanagement;

import Entity.BillSellDetails;
import Entity.Bills;
import Entity.Customer;
import Entity.Items;
import interact.iCheckForm;
import com.toedter.calendar.JCalendar;
import com.toedter.calendar.JDateChooser;
import interact.iLogin;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import java.util.Date;
import model.ColorTable;

/**
 *
 * @author Home
 */
public class frmProcedureBill extends javax.swing.JFrame {

   // private boolean saleBill;
    ArrayList<Entity.Items> items;
    ArrayList<Entity.Customer> cus;
    private ArrayList<Entity.BillSellDetails> alDetails;
    private ArrayList<Float> alPrice;
    ArrayList<Entity.BillSellDetails> arrReceipt;
    ArrayList<Entity.BillSellDetails> arrReceiptProduce;
    static String code = null;

    /** Creates new form frmProcedureBill */
    public frmProcedureBill() {
        initComponents();
        this.setLocation();
        items = DataAccess.Item.getAll();
        cus = DataAccess.Customer.getAllCustomer();
        alDetails = new ArrayList<BillSellDetails>();
        alPrice = new ArrayList<Float>();
        arrReceipt = new ArrayList<BillSellDetails>();
        arrReceiptProduce = new ArrayList<BillSellDetails>();
        txtDiscount.setText("0");
        txtQuantity.setText("1");
        lblSaleID.setText(iLogin.getAccountID() + "");
        lblSaleName.setText(iLogin.getUsername() + "");
        dtcFrom.setEnabled(false);
//        txtCustomerID1.setEditable(true);
//        btnBrowse.setEnabled(true);

        this.refresh();
        this.refresh1();
        this.refresh2();
        this.Refresh3();
    }

    private void setLocation() {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((d.getWidth() - getWidth()) / 2);
        int y = (int) ((d.getHeight() - getHeight()) / 2);
        setLocation(x, y);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCustomerID = new javax.swing.JTextField();
        btnCustomerBrowse = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtItemID = new javax.swing.JTextField();
        btnItemBrowse = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        txtQuantity = new javax.swing.JTextField();
        btnAddBill = new javax.swing.JButton();
        btnDeleteBill = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblBillDetails = new ColorTable();
        jLabel15 = new javax.swing.JLabel();
        lblSaleID = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        lblSaleName = new javax.swing.JLabel();
        txtDiscount = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        cboPayment = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        btnCreateBill = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        btnSearchBy = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        txtCustomerID1 = new javax.swing.JTextField();
        btnBrowse = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        dtcFrom = new com.toedter.calendar.JDateChooser();
        cboSearchBy = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblBill = new ColorTable();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblBillDetail1 = new ColorTable();
        jPanel6 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        txtBillID = new javax.swing.JTextField();
        btnBrowseBill = new javax.swing.JButton();
        jPanel7 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        txtBillIDBillDetails = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtItemIDBillDetails = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtQuantityBillDetails = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        txtQuantityReceiptBillDetails = new javax.swing.JTextField();
        btnUpdate = new javax.swing.JToggleButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        tblBillDetailsReceipt = new ColorTable();
        btnReceiptProduce = new javax.swing.JButton();
        btnSearchBillDetails = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Bills Management");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(51, 153, 255), null));

        jLabel1.setText("Customer ID:");

        btnCustomerBrowse.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/search.gif"))); // NOI18N
        btnCustomerBrowse.setText("Browse");
        btnCustomerBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCustomerBrowseActionPerformed(evt);
            }
        });

        jLabel2.setText("Item ID:");

        btnItemBrowse.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/search.gif"))); // NOI18N
        btnItemBrowse.setText("Browse");
        btnItemBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnItemBrowseActionPerformed(evt);
            }
        });

        jLabel7.setText("Quantity:");

        btnAddBill.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Add.png"))); // NOI18N
        btnAddBill.setText("Add");
        btnAddBill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddBillActionPerformed(evt);
            }
        });

        btnDeleteBill.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Symbol-Delete.png"))); // NOI18N
        btnDeleteBill.setText("Delete");
        btnDeleteBill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteBillActionPerformed(evt);
            }
        });

        tblBillDetails.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblBillDetails);

        jLabel15.setText("Sale ID:");

        jLabel16.setText("Sale Name:");

        txtDiscount.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtDiscountKeyReleased(evt);
            }
        });

        jLabel3.setText("Discount(%):");

        cboPayment.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "---Choosing payment---", "Cash", "Credit Card", "Visa" }));

        jLabel4.setText("Payment:");

        jLabel5.setText("Total:");

        btnCreateBill.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/makeTransfer.png"))); // NOI18N
        btnCreateBill.setText("Create");
        btnCreateBill.setEnabled(false);
        btnCreateBill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateBillActionPerformed(evt);
            }
        });

        btnReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Clear.png"))); // NOI18N
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 704, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtItemID)
                                    .addComponent(txtCustomerID, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btnItemBrowse, 0, 0, Short.MAX_VALUE)
                                    .addComponent(btnCustomerBrowse, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnAddBill)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnDeleteBill))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel16)
                                .addGap(10, 10, 10)
                                .addComponent(lblSaleName, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(37, 37, 37)
                                .addComponent(lblSaleID, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(425, 425, 425)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnCreateBill, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(26, 26, 26)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(cboPayment, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(txtDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCustomerID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCustomerBrowse))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtItemID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnItemBrowse)
                    .addComponent(jLabel7)
                    .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAddBill)
                    .addComponent(btnDeleteBill))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel15)
                            .addComponent(lblSaleID, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblSaleName, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel16))
                        .addGap(67, 67, 67))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cboPayment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnCreateBill, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnReset, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE))))
                .addGap(17, 17, 17))
        );

        jTabbedPane1.addTab("Create Bill", jPanel1);

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(51, 153, 255), null));

        jLabel6.setText("Date:");

        btnSearchBy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/searchsearchsearch.png"))); // NOI18N
        btnSearchBy.setText("Search");
        btnSearchBy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchByActionPerformed(evt);
            }
        });

        jLabel9.setText("Customer ID:");

        txtCustomerID1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCustomerID1KeyReleased(evt);
            }
        });

        btnBrowse.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/search.gif"))); // NOI18N
        btnBrowse.setText("Browse");
        btnBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseActionPerformed(evt);
            }
        });

        cboSearchBy.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CustomerID", "Date" }));
        cboSearchBy.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboSearchByItemStateChanged(evt);
            }
        });

        jLabel8.setText("Search by:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addComponent(dtcFrom, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 339, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel8)))
                        .addGap(18, 18, 18)
                        .addComponent(cboSearchBy, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtCustomerID1, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnBrowse)
                        .addGap(227, 227, 227)))
                .addGap(18, 18, 18)
                .addComponent(btnSearchBy)
                .addGap(48, 48, 48))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(cboSearchBy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnSearchBy))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel9)
                                .addComponent(txtCustomerID1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(btnBrowse))
                        .addGap(18, 18, 18)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dtcFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(51, 153, 255), null));

        tblBill.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblBill.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblBillMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblBill);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(51, 153, 255), null));

        tblBillDetail1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(tblBillDetail1);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Search Bill", jPanel2);

        jLabel10.setText("Bill ID:");

        txtBillID.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtBillIDKeyReleased(evt);
            }
        });

        btnBrowseBill.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/search.gif"))); // NOI18N
        btnBrowseBill.setText("Browse");
        btnBrowseBill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseBillActionPerformed(evt);
            }
        });

        jPanel7.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(51, 153, 255), null));

        jLabel11.setText("Bill ID:");

        txtBillIDBillDetails.setEditable(false);

        jLabel12.setText("Item ID:");

        txtItemIDBillDetails.setEditable(false);

        jLabel13.setText("Quantity:");

        txtQuantityBillDetails.setEditable(false);

        jLabel14.setText("Quantity Receipt:");

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/update.png"))); // NOI18N
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        tblBillDetailsReceipt.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblBillDetailsReceipt.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblBillDetailsReceiptMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(tblBillDetailsReceipt);

        btnReceiptProduce.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/makeTransfer.png"))); // NOI18N
        btnReceiptProduce.setText("Receipt Produce");
        btnReceiptProduce.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReceiptProduceActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel7Layout.createSequentialGroup()
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE)
                            .addContainerGap())
                        .addGroup(jPanel7Layout.createSequentialGroup()
                            .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel12)
                                .addComponent(jLabel11))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtBillIDBillDetails, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                                .addComponent(txtItemIDBillDetails, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE))
                            .addGap(18, 18, 18)
                            .addComponent(jLabel13)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(txtQuantityBillDetails, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                            .addGap(18, 18, 18)
                            .addComponent(jLabel14)
                            .addGap(18, 18, 18)
                            .addComponent(txtQuantityReceiptBillDetails, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(btnUpdate)
                            .addGap(18, 18, 18)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                        .addComponent(btnReceiptProduce)
                        .addGap(30, 30, 30))))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtBillIDBillDetails, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(txtItemIDBillDetails, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13)
                    .addComponent(txtQuantityBillDetails, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14)
                    .addComponent(txtQuantityReceiptBillDetails, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnUpdate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnReceiptProduce)
                .addGap(35, 35, 35))
        );

        btnSearchBillDetails.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/searchsearchsearch.png"))); // NOI18N
        btnSearchBillDetails.setText("Search");
        btnSearchBillDetails.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchBillDetailsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtBillID, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnBrowseBill)
                        .addGap(33, 33, 33)
                        .addComponent(btnSearchBillDetails))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtBillID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBrowseBill)
                    .addComponent(btnSearchBillDetails))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, 337, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Receipt Produce", jPanel6);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 728, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 430, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCustomerBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCustomerBrowseActionPerformed
        // TODO add your handling code here:
        frmCustomerBrowseDialog2 frmCusBrow = new frmCustomerBrowseDialog2(this, true);
        frmCusBrow.code = this;
        frmCusBrow.setVisible(true);
    }//GEN-LAST:event_btnCustomerBrowseActionPerformed

    private void btnItemBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnItemBrowseActionPerformed
        // TODO add your handling code here:
        frmItemBrowseDialog1 frmIteBrow = new frmItemBrowseDialog1(this, true);
        frmIteBrow.code = this;
        frmIteBrow.setVisible(true);
    }//GEN-LAST:event_btnItemBrowseActionPerformed

    private void btnAddBillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddBillActionPerformed
        // TODO add your handling code here:
        String iQuantity = txtQuantity.getText();

        String itemID = txtItemID.getText();
        if (itemID.equals("")) {
            JOptionPane.showMessageDialog(this, "ItemID is not null!", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtItemID.requestFocus();
            return;
        } else if (iCheckForm.checkNumber(itemID)) {
            JOptionPane.showMessageDialog(this, "Invalid Format", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtItemID.requestFocus();
            return;
        } else {
            boolean itemFound = false;
            Entity.Items it = new Items();
            System.out.println(Integer.parseInt(itemID));
            for (Items items1 : items) {
                if (items1.getItemsID() == Integer.parseInt(itemID)) {
                    itemFound = true;
                    it = items1;
                    break;
                }
            }

            System.out.println(it.toString());
            System.out.println(itemFound);

            if (itemFound == false) {
                JOptionPane.showMessageDialog(this, "ItemID is not exist!", "ERROR", JOptionPane.ERROR_MESSAGE);
                txtItemID.requestFocus();
                return;
            } else {
                //iQuantity = Integer.valueOf(txtQuantity.getText());
                if (iQuantity.equals("")) {
                    JOptionPane.showMessageDialog(this, "Quantity is not null!", "ERROR", JOptionPane.ERROR_MESSAGE);
                    txtQuantity.requestFocus();
                    return;
                }
                if (iCheckForm.checkNumber(iQuantity)) {
                    JOptionPane.showMessageDialog(this, "Quantity Invalid Format", "ERROR", JOptionPane.ERROR_MESSAGE);
                    txtQuantity.requestFocus();
                    return;
                }
                if (Integer.parseInt(iQuantity) > it.getQuantityAvailable()) {
                    JOptionPane.showMessageDialog(this, "This product doesn't enough quantity", "Error", JOptionPane.ERROR_MESSAGE);
                    txtQuantity.requestFocus();
                    return;
                }
                if (Integer.valueOf(iQuantity) <= 0) {
                    JOptionPane.showMessageDialog(this, "Quantity is more than zero.", "Error", JOptionPane.ERROR_MESSAGE);
                    txtQuantity.requestFocus();
                    return;
                }

                boolean bFound = false;
                for (Entity.BillSellDetails billSellDetails : alDetails) {
                    if (billSellDetails.getItemID() == it.getItemsID()) {
                        billSellDetails.setQuantity(billSellDetails.getQuantity() + Integer.parseInt(iQuantity));
                        it.setQuantityAvailable(it.getQuantityAvailable() - Integer.parseInt(iQuantity));
                        System.out.println(it.getQuantityAvailable());
                        bFound = true;
                        break;
                    }
                }

                if (bFound == false) {
                    Entity.BillSellDetails bdetail = new BillSellDetails();
                    bdetail.setItemID(it.getItemsID());
                    float ItemPrice = it.getPrice();
                    System.out.println(ItemPrice);
                    bdetail.setQuantity(Integer.parseInt(iQuantity));
                    it.setQuantityAvailable(it.getQuantityAvailable() - Integer.parseInt(iQuantity));
                    System.out.println(it.getQuantityAvailable());
                    alDetails.add(bdetail);
                    alPrice.add(ItemPrice);
                }
                btnCreateBill.setEnabled(true);
            }

            this.refresh();
        }
    }//GEN-LAST:event_btnAddBillActionPerformed

//    private boolean checkCustomerID(String value) {
////        if (!value.equals("")) {
//        System.out.println("------------------");
//        int CustomerID = 0;
//
//        if (iCheckForm.checkNumber(value)) {
//            JOptionPane.showMessageDialog(this, "CustomerID Invalid fomat");
//            return false;
//
//        } else {
////            JOptionPane.showMessageDialog(this, "CustomerID is not null", "ERROR", JOptionPane.ERROR_MESSAGE);
//            try {
//                CustomerID = Integer.valueOf(txtCustomerID.getText());
//            } catch (NumberFormatException e) {
//                JOptionPane.showMessageDialog(this, "Customer id is not null.");
//                return false;
//            }
////            return false;
//        }
//        return true;
//    }
    private void btnCreateBillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateBillActionPerformed
        // TODO add your handling code here:
        boolean testCode = false;
        String CustomerID = txtCustomerID.getText();
        String itemID = txtItemID.getText();

        if (CustomerID.equals("")) {
            JOptionPane.showMessageDialog(this, "CustomerID is not null!", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtCustomerID.requestFocus();
            return;
        } else if (iCheckForm.checkNumber(CustomerID)) {
            JOptionPane.showMessageDialog(this, "CustomerID Invalid Format", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtCustomerID.requestFocus();
            return;
        }
//        if (itemID.equals("")) {
//            JOptionPane.showMessageDialog(this, "ItemID is not null!", "ERROR", JOptionPane.ERROR_MESSAGE);
//            txtItemID.requestFocus();
//            return;
//        } else
//        if (iCheckForm.checkNumber(itemID)) {
//            JOptionPane.showMessageDialog(this, "ItemID Invalid Format", "ERROR", JOptionPane.ERROR_MESSAGE);
//            txtItemID.requestFocus();
//            return;
//        }

        for (Entity.Customer customer : cus) {
            if (customer.getCustomerID() == Integer.parseInt(CustomerID)) {
                testCode = true;
                break;
            }
        }
        if (testCode == false) {
            JOptionPane.showMessageDialog(this, "Customer id is not exist.");
            return;
        }

        float disCount = 0;
        try {
            disCount = Float.valueOf(txtDiscount.getText());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "Discount fomat invalid.");
            return;
        }

        if (disCount < 0 || disCount > 100) {
            JOptionPane.showMessageDialog(this, "Discount fomat invalid.");
            return;
        }
        if (alDetails == null) {
            JOptionPane.showMessageDialog(this, "Please enter items.");
            return;
        }
        if (cboPayment.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Please choosing payment.");
            return;
        }
        Entity.Bills bill = new Bills();
        bill.setAccountID(interact.iLogin.getAccountID());
        //bill.setAccountID(2);
        bill.setCustomerID(Integer.parseInt(CustomerID));
        bill.setDate(Entity.DateUtils.now("MM/dd/yyyy"));
        bill.setDiscount(disCount);
        bill.setPayment(String.valueOf(cboPayment.getSelectedItem()));

        System.out.println(bill.toString());

        for (BillSellDetails billSellDetails : alDetails) {
            System.out.println(billSellDetails.toString());
        }
        try {
            DataAccess.Bills.insertBill(bill, alDetails);
//            JOptionPane.showMessageDialog(this, "Create Bill Successfull!", code, CustomerID);
            JOptionPane.showMessageDialog(this, "Create Bill Successfull!");
            resetform();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Cannot create a new bill");
            return;
        }
    }//GEN-LAST:event_btnCreateBillActionPerformed

    private void txtDiscountKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDiscountKeyReleased
        // TODO add your handling code here:
        calcTotal();
    }//GEN-LAST:event_txtDiscountKeyReleased

    private void btnDeleteBillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteBillActionPerformed
        // TODO add your handling code here:
        int iSelected = tblBillDetails.getSelectedRow();
        System.out.println("        " + iSelected);
        if (iSelected < 0) {
            JOptionPane.showMessageDialog(this, "Please choose one.");
            return;
        } else {
            Entity.Items it = new Items();
            it.setItemsID((Integer) tblBillDetails.getValueAt(iSelected, 0));
            //it.setPrice((Float) tblBillDetails.getValueAt(iSelected, 1));
            it.setQuantityAvailable((Integer) tblBillDetails.getValueAt(iSelected, 2));
            System.out.println("it" + it.toString());

            for (Items item : items) {
                if (item.getItemsID() == it.getItemsID()) {
                    item.setQuantityAvailable(item.getQuantityAvailable() + it.getQuantityAvailable());
                    System.out.println("------------" + item.getQuantityAvailable());
                }
            }
            alDetails.remove(iSelected);
            alPrice.remove(iSelected);
            if (alDetails.size() == 0) {
                btnCreateBill.setEnabled(false);
            }
            refresh();
        }

    }//GEN-LAST:event_btnDeleteBillActionPerformed

    private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseActionPerformed
        // TODO add your handling code here:
        frmCustomerBrowseDialog1 frmcusbrow = new frmCustomerBrowseDialog1(this, true);
        frmcusbrow.code = this;
        frmcusbrow.setVisible(true);
    }//GEN-LAST:event_btnBrowseActionPerformed

    private void txtCustomerID1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCustomerID1KeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCustomerID1KeyReleased

    private void btnSearchByActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchByActionPerformed
        // TODO add your handling code here:
        boolean bFoundBill = false;
        boolean bFoundCus = false;
        if (cboSearchBy.getSelectedIndex() == 0) {
            System.out.println("CustomerID");
            String str = txtCustomerID1.getText();

            if (str.equals("")) {
                JOptionPane.showMessageDialog(this, "ItemID is not null!", "ERROR", JOptionPane.ERROR_MESSAGE);
                txtCustomerID1.requestFocus();
                return;
            } else if (iCheckForm.checkNumber(str)) {
                JOptionPane.showMessageDialog(this, "Invalid Format", "ERROR", JOptionPane.ERROR_MESSAGE);
                txtCustomerID1.requestFocus();
                return;
            }
            if (Integer.parseInt(txtCustomerID1.getText()) <= 0) {
                JOptionPane.showMessageDialog(this, "CustomerID must more than zero.");
                txtCustomerID1.requestFocus();
                return;
            }

            try {
                cus = DataAccess.Customer.getAllCustomer();
                for (Customer custm : cus) {
                    if (custm.getCustomerID() == Integer.parseInt(str)) {
                        bFoundCus = true;
                    }
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            if (bFoundCus) {
                String sql = "select * from Bills where Status=1 and CustomerID=" + Integer.valueOf(str);
                try {
                    bFoundBill = DataAccess.TableAccess.readToTableBill(sql, tblBill);
                    System.out.println(bFoundBill);
                    if (bFoundBill == false) {
                        JOptionPane.showMessageDialog(this, "Customer have no bill", "Infomation", JOptionPane.INFORMATION_MESSAGE);
                    }
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            } else {
                JOptionPane.showMessageDialog(this, "CustomerID is not exist", "ERROR", JOptionPane.ERROR_MESSAGE);
                txtCustomerID1.requestFocus();
                return;
            }

        } else {
            System.out.println("Date");
            String date = "";
            Date dt = dtcFrom.getDate();
            final SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy");
            try {
                date = SDF.format(dt).toString();
            } catch (NullPointerException e) {
                JOptionPane.showMessageDialog(this, "Date null");
                dtcFrom.requestFocus();
                return;
            }
            String sql = "select * from bills where Date='" + date + "'";
            try {
                DataAccess.TableAccess.readToTableBill(sql, tblBill);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }//GEN-LAST:event_btnSearchByActionPerformed

    private void tblBillMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblBillMouseClicked
        // TODO add your handling code here:
        int isSelected = tblBill.getSelectedRow();
        if (isSelected < 0) {
            JOptionPane.showConfirmDialog(this, "Please choose one.");
            return;
        } else {
            //Entity.Bills bil = new Bills();
            //bil.setBillID(tblBill.getValueAt(isSelected, 0));
            int id = Integer.parseInt(tblBill.getValueAt(isSelected, 0).toString());

            System.out.println(id);
            String sql = "select * from BillSellDetails where BillID=" + id;
            try {
                DataAccess.TableAccess.readToTable(sql, tblBillDetail1);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }//GEN-LAST:event_tblBillMouseClicked

    private void btnBrowseBillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseBillActionPerformed
        // TODO add your handling code here:
        frmBillBrowseDialog frmbill = new frmBillBrowseDialog(this, true);
        frmbill.code = this;
        frmbill.setVisible(true);
    }//GEN-LAST:event_btnBrowseBillActionPerformed

    private void txtBillIDKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBillIDKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_txtBillIDKeyReleased

    private void btnSearchBillDetailsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchBillDetailsActionPerformed
        // TODO add your handling code here:
        boolean bFound = false;
        String str = txtBillID.getText();
        if (iCheckForm.checkNumber(txtBillID.getText())) {
            JOptionPane.showMessageDialog(this, "Invalid fomat");
            txtBillID.requestFocus();
            return;
        }
        if (Integer.parseInt(txtBillID.getText()) <= 0) {
            JOptionPane.showMessageDialog(this, "CustomerID must more than zero.");
            txtBillID.requestFocus();
            return;
        }
        if (str.equals("")) {
            JOptionPane.showMessageDialog(this, "Quantity receipt is not null", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtQuantityReceiptBillDetails.requestFocus();
            return;
        }
        String sql = "select * from BillSellDetails where BillID=" + Integer.parseInt(str);
        try {
            bFound = DataAccess.TableAccess.readToReceiptTable(sql, tblBillDetailsReceipt);
            System.out.println(bFound);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        if (!bFound) {
            JOptionPane.showMessageDialog(this, "Bill is not Exist", "Infomation", JOptionPane.INFORMATION_MESSAGE);
        } else {
            arrReceipt = DataAccess.BillSellDetails.getAllByID(Integer.parseInt(txtBillID.getText()));
            for (BillSellDetails billSellDetails : arrReceipt) {
                System.out.println(billSellDetails.toString());
            }
            refresh1();
        }


    }//GEN-LAST:event_btnSearchBillDetailsActionPerformed

    private void tblBillDetailsReceiptMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblBillDetailsReceiptMouseClicked
        // TODO add your handling code here:
        int isSelected = tblBillDetailsReceipt.getSelectedRow();
        try {

            txtBillIDBillDetails.setText(String.valueOf(tblBillDetailsReceipt.getValueAt(isSelected, 0)));
            txtItemIDBillDetails.setText(String.valueOf(tblBillDetailsReceipt.getValueAt(isSelected, 1)));
            txtQuantityBillDetails.setText(String.valueOf(tblBillDetailsReceipt.getValueAt(isSelected, 2)));
            txtQuantityReceiptBillDetails.setText(String.valueOf(tblBillDetailsReceipt.getValueAt(isSelected, 3)));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }//GEN-LAST:event_tblBillDetailsReceiptMouseClicked

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:
        String str = txtQuantityReceiptBillDetails.getText();
        int isSelected = tblBillDetailsReceipt.getSelectedRow();
        if (isSelected == -1) {
            JOptionPane.showMessageDialog(this, "Please choose one.", "ERROR", JOptionPane.ERROR_MESSAGE);
            return;
        } else {
            if (iCheckForm.checkNumber((txtQuantityReceiptBillDetails.getText()))) {
                JOptionPane.showMessageDialog(this, "Invalid fomat");
                return;
            }
            if (str.equals("")) {
                JOptionPane.showMessageDialog(this, "Quantity receipt is not null", "ERROR", JOptionPane.ERROR_MESSAGE);
                txtQuantityReceiptBillDetails.requestFocus();
                return;
            }
            if (Integer.parseInt(str) <= 0) {
                JOptionPane.showMessageDialog(this, "Quantity Receipt must more than zero", "ERROR", JOptionPane.ERROR_MESSAGE);
                txtQuantityReceiptBillDetails.requestFocus();
                return;
            }
            if (Integer.parseInt(str) > Integer.parseInt(txtQuantityBillDetails.getText())) {
                JOptionPane.showMessageDialog(this, "Quantity Receipt must less than Quantity", "ERROR", JOptionPane.ERROR_MESSAGE);
                txtQuantityReceiptBillDetails.requestFocus();
                return;
            }
            int itemIDReceipt = Integer.parseInt(txtItemIDBillDetails.getText());
            int quantityReceipt = Integer.parseInt(txtQuantityReceiptBillDetails.getText());
            for (BillSellDetails billSellDetails : arrReceipt) {
                if (billSellDetails.getItemID() == itemIDReceipt) {
                    billSellDetails.setQuantityReceipt(quantityReceipt);
                    arrReceiptProduce.add(billSellDetails);
                    break;
                }
            }
//            int billID = Integer.parseInt(txtBillIDBillDetails.getText());
//
//            Entity.Bills bil = new Bills();
//            bil.setBillID(billID);
//            for (BillSellDetails billSellDetails : arrReceipt) {
//                if (billSellDetails.getQuantityReceipt() > 0) {
//                    arrReceiptProduce.add(billSellDetails);
//                }
//            }
            refresh1();
        }

    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnReceiptProduceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReceiptProduceActionPerformed
        // TODO add your handling code here:
        //arrReceiptProduce.clear();
        int i = arrReceipt.size();
        int j = arrReceiptProduce.size();
        if (i == 0) {
            JOptionPane.showMessageDialog(this, "You must choose Bill ID to receipt produce.", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtBillIDBillDetails.requestFocus();
            return;
        }
        if (j == 0) {
            JOptionPane.showMessageDialog(this, "You must set quantity receipt for item to receipt produce.", "ERROR", JOptionPane.ERROR_MESSAGE);
            txtQuantityReceiptBillDetails.requestFocus();
            return;
        }
        int billID = Integer.parseInt(txtBillIDBillDetails.getText());

        Entity.Bills bil = new Bills();
        bil.setBillID(billID);
//        for (BillSellDetails billSellDetails : arrReceipt) {
//            if (billSellDetails.getQuantityReceipt() > 0) {
//                arrReceiptProduce.add(billSellDetails);
//            }
//        }

        for (BillSellDetails billSellDetails : arrReceiptProduce) {
            System.out.println(billSellDetails.toString());
        }
        try {
            DataAccess.ReceiptProduce.insertReceiptProduce(arrReceiptProduce, bil);
            JOptionPane.showMessageDialog(this, "Receipt produce Successfull", "Infomation", JOptionPane.INFORMATION_MESSAGE);
            resetform();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Receipt produce is not Successful", "ERROR", JOptionPane.ERROR_MESSAGE);
            return;
        }

    }//GEN-LAST:event_btnReceiptProduceActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        // TODO add your handling code here:
        resetform();
    }//GEN-LAST:event_btnResetActionPerformed

    private void cboSearchByItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboSearchByItemStateChanged
        // TODO add your handling code here:
        if (cboSearchBy.getSelectedIndex() == 0) {
            dtcFrom.setEnabled(false);
            txtCustomerID1.setEditable(true);
            btnBrowse.setEnabled(true);
        } else {
            dtcFrom.setEnabled(true);
            txtCustomerID1.setEditable(false);
            btnBrowse.setEnabled(false);
        }
        
    }//GEN-LAST:event_cboSearchByItemStateChanged

    private void refresh() {
        tblBillDetails.setModel(new model.BillDetails(alDetails, alPrice));
        calcTotal();
    }

    private void refresh1() {
        tblBillDetailsReceipt.setModel(new model.BillDetailsReceipt(arrReceipt));
    }

    private void refresh2() {
        arrReceipt.clear();
        tblBillDetailsReceipt.setModel(new model.BillDetailsReceipt(arrReceipt));
    }
    private void Refresh3()
    {
        String sql = "select * from Bills where Status=1 and CustomerID=''";
        String sql1 = "select * from BillSellDetails where BillID=''";
                try {
                    DataAccess.TableAccess.readToTableBill(sql, tblBill);
                    DataAccess.TableAccess.readToTable(sql1, tblBillDetail1);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
    }

    public void resetform() {
        txtCustomerID.setText("");
        txtDiscount.setText("0");
        txtQuantity.setText("1");
        txtItemID.setText("");
        alDetails.clear();
        alPrice.clear();
        txtBillIDBillDetails.setText("");
        txtBillID.setText("");
        txtItemIDBillDetails.setText("");
        txtQuantityBillDetails.setText("");
        txtQuantityReceiptBillDetails.setText("0");
        tblBillDetailsReceipt.removeAll();
        cboPayment.setSelectedIndex(0);
        refresh();
        refresh2();
    }

    private float calcTotal() {
        float dTotal = 0;

        Entity.BillSellDetails bd = new BillSellDetails();
        for (int i = 0; i < alDetails.size(); i++) {
            bd = (BillSellDetails) alDetails.get(i);
            dTotal += alPrice.get(i) * bd.getQuantity();
        }

        double dDiscount = 0;
        try {
            dDiscount = Double.valueOf(txtDiscount.getText());
        } catch (Exception ex) {
        }
        dDiscount = Math.min(Math.max(dDiscount, 0), 100);
        System.out.println("discount:" + dDiscount);
        dTotal *= (100 - dDiscount) / 100;
        lblTotal.setText(dTotal + "");

        return dTotal;
    }

    public static boolean isNumberic(String value) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(value);
        if (!matcher.find()) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(frmProcedureBill.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(frmProcedureBill.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(frmProcedureBill.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(frmProcedureBill.class.getName()).log(Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmProcedureBill().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddBill;
    private javax.swing.JButton btnBrowse;
    private javax.swing.JButton btnBrowseBill;
    private javax.swing.JButton btnCreateBill;
    private javax.swing.JButton btnCustomerBrowse;
    private javax.swing.JButton btnDeleteBill;
    private javax.swing.JButton btnItemBrowse;
    private javax.swing.JButton btnReceiptProduce;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSearchBillDetails;
    private javax.swing.JButton btnSearchBy;
    private javax.swing.JToggleButton btnUpdate;
    private javax.swing.JComboBox cboPayment;
    private javax.swing.JComboBox cboSearchBy;
    private com.toedter.calendar.JDateChooser dtcFrom;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lblSaleID;
    private javax.swing.JLabel lblSaleName;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JTable tblBill;
    private javax.swing.JTable tblBillDetail1;
    private javax.swing.JTable tblBillDetails;
    private javax.swing.JTable tblBillDetailsReceipt;
    public javax.swing.JTextField txtBillID;
    private javax.swing.JTextField txtBillIDBillDetails;
    public javax.swing.JTextField txtCustomerID;
    public javax.swing.JTextField txtCustomerID1;
    private javax.swing.JTextField txtDiscount;
    public javax.swing.JTextField txtItemID;
    private javax.swing.JTextField txtItemIDBillDetails;
    private javax.swing.JTextField txtQuantity;
    private javax.swing.JTextField txtQuantityBillDetails;
    private javax.swing.JTextField txtQuantityReceiptBillDetails;
    // End of variables declaration//GEN-END:variables
}
